import api from './api'

export interface LoginData {
  email: string
  password: string
}

export interface RegisterData {
  email: string
  username: string
  password: string
}

export const authService = {
  // 登录
  login: (data: LoginData) => api.post('/auth/login', data),
  
  // 注册
  register: (data: RegisterData) => api.post('/auth/register', data),
  
  // 获取当前用户信息
  getCurrentUser: () => api.get('/auth/me'),
  
  // 验证邮箱
  verifyEmail: (token: string) => api.get(`/auth/verify-email/${token}`),
}

export const userService = {
  // 获取用户资料
  getProfile: () => api.get('/users/profile'),
  
  // 更新用户资料
  updateProfile: (data: any) => api.put('/users/profile', data),
  
  // 上传头像
  uploadAvatar: (formData: FormData) => {
    return api.post('/users/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    })
  },
}

export const courseService = {
  // 获取课程列表
  getCourses: (params?: any) => api.get('/courses', { params }),
  
  // 获取课程详情
  getCourseById: (id: string) => api.get(`/courses/${id}`),
  
  // 报名课程
  enrollCourse: (id: string) => api.post(`/courses/${id}/enroll`),
  
  // 获取我的已报名课程
  getEnrolledCourses: () => api.get('/courses/my/enrolled'),
  
  // 更新学习进度
  updateProgress: (lessonId: string, data: any) => 
    api.post(`/courses/lessons/${lessonId}/progress`, data),
  
  // 创建课程(教师)
  createCourse: (data: any) => api.post('/courses', data),
  
  // 更新课程
  updateCourse: (id: string, data: any) => api.put(`/courses/${id}`, data),
  
  // 删除课程
  deleteCourse: (id: string) => api.delete(`/courses/${id}`),
}

export const orderService = {
  // 创建订单
  createOrder: (data: { courseId: string }) => api.post('/orders', data),
  
  // 获取我的订单
  getMyOrders: () => api.get('/orders/my'),
  
  // 获取订单详情
  getOrderById: (id: string) => api.get(`/orders/${id}`),
}

export const discussionService = {
  // 获取讨论列表
  getDiscussions: (params?: any) => api.get('/discussions', { params }),
  
  // 获取讨论详情
  getDiscussionById: (id: string) => api.get(`/discussions/${id}`),
  
  // 创建讨论
  createDiscussion: (data: { title: string; content: string }) => 
    api.post('/discussions', data),
  
  // 创建回复
  createReply: (discussionId: string, data: { content: string; parentId?: string }) => 
    api.post(`/discussions/${discussionId}/replies`, data),
}
